package core.online;

import core.Entity;
import core.ViewableEntity;

/**
 * This is a viewable entity whose state persists across all computers on the
 * network. It has a built-in smoothing algorithm for making updates from the
 * server in order to not look like a huge jolt whenever a new packet arrives.
 * 
 * @author Jason
 * 
 */
public abstract class OnlineEntity extends ViewableEntity {

	protected Entity serverCopy;

	@Override
	public void tick(double seconds) {
		super.tick(seconds);
		if (serverCopy != null) {
			serverCopy.tick(seconds);
			// move this entity half-way to the other one
			x = (x + serverCopy.x) / 2;
			y = (y + serverCopy.y) / 2;
			if (this.equals(serverCopy)) {
				// we don't have to adjust anymore,
				// it's close enough
				serverCopy = null;
			}
		}
	}

	/**
	 * Tells this entity to smoothly move until it is equal to the given server
	 * copy of itself.
	 * 
	 * @param serverCopy
	 *            The state of this entity on the server.
	 */
	public void driftTo(Entity serverCopy) {
		velocityHeading = serverCopy.velocityHeading;
		velocityMagnitude = serverCopy.velocityMagnitude;
		super.rotationSpeed = serverCopy.rotationSpeed;
		if (this.equals(serverCopy) == false)
			this.serverCopy = serverCopy;
	}

}
